import config from "../config"

//px 转换 vw
export function pxToVw(px) {
    return (px / config.uiWidth * 100) + 'vw'
}

export function formatNumber(num) {
    let groups = (/([\-\+]?)(\d*)(\.\d+)?/g).exec("" + num),
        mask = groups[1],
        integers = (groups[2] || "").split(""),
        decimal = groups[3] || "",
        remain = integers.length % 3;

    let temp = integers.reduce((previousValue, currentValue, index) => {
        if (index + 1 === remain || (index + 1 - remain) % 3 === 0) {
            return previousValue + currentValue + ",";
        } else {
            return previousValue + currentValue;
        }
    }, "").replace(/\,$/g, "");
    return mask + temp + decimal;
}

export function calcBasis(now, old) {
    const temp = (now - old) / (old / 100);
    if(temp === Infinity && old===0) return 100
    return temp ? Number(temp).toFixed(0) :0
}

export function calcBasisIncrease(now, old) {
    let res = calcBasis(now, old)
    let str = ' $ ';
    if(res) {
        str += res < 0 ? '↓' : "↑";
        res = Math.abs(res);
    }
    return str.replace('$', res?`${res}%`:"无");
}

export function getPercent(num, total) {
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}
